import { _decorator, Component, find, Label, Node, NodeEventType } from 'cc';
import { GameData } from '../GameData';
import { JokerMgr } from '../Function/JokerMgr';
const { ccclass, property } = _decorator;

@ccclass('Mark')
export class Mark extends Component {
    private needGoldnum:number=0;
    private sellGoldnum:number=0;
    private PriceLabel:Label=null;
    private isEquipped:boolean=false;
    init(needGoldnum,sellGoldnum){
        this.needGoldnum=needGoldnum;
        this.sellGoldnum=sellGoldnum;
        this.PriceLabel=this.node.children[0].getComponent(Label);
        //写好标签
        this.PriceLabel.string=this.needGoldnum+"$";
        //注册购买售卖事件
        this.node.on(NodeEventType.MOUSE_DOWN,this.buyJokerAndSell,this)
        //将自身隐藏
        this.node.active=false;
    }
    showPrice(isEquipped){
        this.node.active=true;
        if(isEquipped){
            this.PriceLabel.string=this.sellGoldnum+"$";
        }
        else{
            this.PriceLabel.string=this.needGoldnum+"$";
        }
    }
    closePrice(){
        this.node.active=false;
    }
    buyJokerAndSell(){
        //未装备则购买
        if(!this.isEquipped){
            if(this.needGoldnum>GameData.coin){
                //钱不够
                return;
            }
            GameData.coin-=this.needGoldnum;
            //将Joker移入JokerMgr
            let jokerMgr=this.node.parent.parent.parent.parent.getChildByName("JokerMgr");
            this.node.parent.parent=jokerMgr;
            let jokerMgrts=jokerMgr.getComponent(JokerMgr);
            let pos=(jokerMgrts.Jokers.values.length*150)-300;
            this.node.parent.setPosition(pos,0);
            this.isEquipped=true;
            this.closePrice();
            return;
        }
        //已装备则售卖
        this.node.parent.destroy();
        GameData.coin+=this.sellGoldnum;
    }
}


